package dataStructBinaryTree;

import java.util.ArrayList;
import java.util.List;

public class MyBinaryTree {

    static class TreeNode{
        public char val;  // 数据域
        public TreeNode left;  //左孩子引用
        public TreeNode right; // 有孩子引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode root; // 根节点

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        this.root = A;
        return root;
    }

    // 前序遍历
    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }


    // 中序遍历
    void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }


    // 后序遍历
    void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

//    public List<Integer> preorderTraversal(TreeNode root) {
//        if (root == null){
//            return null;
//        }
//        List<Integer> ret = new ArrayList<>();
//        ret.add(root);
//        List<Integer> leftNode = preorderTraversal(root);
//        ret.addAll(leftNode);
//        List<Integer> rightNode = preorderTraversal(root);
//        ret.addAll(rightNode);
//        return ret;
//    }


    // 获取树中节点的个数
    public int size(TreeNode root){
        if(root == null){
            return 0;
        }
        int count = 1;
        int leftCount = size(root.left);
        int rightCount = size(root.right);
        count = count + leftCount + rightCount;
        return count;
    }

//    public int usedSize;
//    public void size2(TreeNode root){
//        if(root == null){
//            return;
//        }
//        usedSize++;
//        preOrder(root.left);
//        preOrder(root.right);
//    }


    // 获取叶子节点的个数
    public int getLeafNodeCount(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right== null){
            return 1;
        }
        int left = getLeafNodeCount(root.left);
        int right = getLeafNodeCount(root.right);
        return left+right;
    }


    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if (root == null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        int left = getKLevelNodeCount(root.left,k-1);
        int right = getKLevelNodeCount(root.right,k-1);
        return left+right;
    }

    // 获取二叉树的高度
    // 求左树的高度和右树的高度 然后取最大值 然后加一  就是树的高度
    int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return left>right ? left+1 : right+1;
    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode left = find(root.left,val);
        if (left != null){
            return left;
        }
        TreeNode right = find(root.right,val);
        if (right != null){
            return right;
        }
        return null;
    }



}
